package com.jrh.web.ajax.pub;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jrh.biz.user.UserService;
import com.jrh.domain.entity.User;
import com.jrh.domain.user.LoginUserInfo;
import com.jrh.web.member.tools.UserAuthUtil;

/**
 * 用户信息ajax
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-23 上午11:09:36
 */
@Controller
@RequestMapping("/user_ajax")
public class UserInfoAjax {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView doAjaxLogin(String loginId, String password, HttpServletRequest request,
                                    HttpServletResponse response) {
        ModelAndView result = new ModelAndView();

        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo != null) {
            result.addObject("_data_", new UserLoginResult(true, userInfo));
            return result;
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        User user = null;
        try {
            user = userService.checkLogin(loginId, password, User.JRH_SELF_USER_SITE);
            UserAuthUtil.signIn(user, request, response);
        } catch (Exception e) {
        }
        userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo != null) {
            result.addObject("_data_", new UserLoginResult(true, userInfo));
            return result;
        }
        result.addObject("_data_", userLoginResult);
        return result;

    }

    /**
     * ajax取得用户信息
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/get_user")
    public ModelAndView getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView result = new ModelAndView();
        result.addObject("user", UserAuthUtil.getUserInfo(request, response));
        return result;
    }

    @RequestMapping("/isExistsLoginId")
    public ModelAndView isExistsLoginId(@RequestParam("loginId") String loginId, HttpServletRequest request,
                                        HttpServletResponse response) {
        ModelAndView result = new ModelAndView();

        User user = userService.getUserByLoginIdAndSite(loginId, null);
        if (user == null) {
            result.addObject("success", Boolean.TRUE);
        } else {
            result.addObject("success", Boolean.FALSE);
        }
        return result;
    }

    @RequestMapping("/isExistsNickname")
    public ModelAndView isExistsNickname(@RequestParam("nickname") String nickname, HttpServletRequest request,
                                         HttpServletResponse response) {
        ModelAndView result = new ModelAndView();

        User user = userService.getUserByNickname(nickname);
        if (user == null) {
            result.addObject("success", Boolean.TRUE);
        } else {
            result.addObject("success", Boolean.FALSE);
        }
        return result;
    }

}
